/*
 * Copyright (C) 2014 Виктор Сытник, <Victor.Sytnik@sovic.org.ua>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ua.org.sovic.oClients.DB;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Виктор Сытник, <Victor.Sytnik@sovic.org.ua>
 */
public class eTypeApp {
    private Dao<ElementType, Integer> typeDao;
    private final static String databaseUrl = "jdbc:sqlite:oClients.db";
    
    
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        new eTypeApp().doMain(args);
    }
    private void doMain(String[] args) throws ClassNotFoundException, SQLException {
        ConnectionSource connectionSource = null;
            try {
                Class.forName("org.sqlite.JDBC");
                connectionSource = new JdbcConnectionSource(databaseUrl);
                setupDatabase(connectionSource);
                //addElementType("Рабочий", ElementType.MAIL);
            } finally {
                if (connectionSource != null)
                    connectionSource.close();
            }
    }
    private void setupDatabase(ConnectionSource connectionSource) throws SQLException {
        typeDao = DaoManager.createDao(connectionSource, ElementType.class);
        if (typeDao.isTableExists()) {
            getAllElements();
        } else {
            TableUtils.createTable(connectionSource, ElementType.class);
        }
    }
    private void getAllElements() throws SQLException {
        List<ElementType> data = typeDao.queryForAll();
        for (ElementType element : data) {
            System.out.println(
                         "ID: " + element.getID() + 
                     " | Тип: " + (element.getType() ? "Почта" : "Телефон") + 
                " | Значение: " + element.getName()
            );
        }
    }
    private void resetDatabase() {
        
    }
    private void addElementType(String _name, boolean _type) throws SQLException {
        ElementType element = new ElementType(_name, _type);
        typeDao.create(element);
        int id = element.getID();
        boolean flag = element.getType();
        String name = element.getName();
        System.out.println(
            "New record ID: " + id + 
                   " | Тип: " + (flag ? "Почта" : "Телефон") + 
              " | Значение: " + name
        );
    }
}
